
// Open MoneySpinner Suite v1
// An open source business management software system written in Java and MySQL
// Recommended IDE is NetBeans IDE 7.0.1
// Support Web Site: http://www.milliscript.com
//
// Copyright (C) 2014, Abiodun Aremu, Ibadan/NIGERIA.
// Open MoneySpinner Suite is distributed under the terms of the Apache License version 2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package openmoneyspinnersuite;

/**
 *
 * @author Abiodun Aremu
 */
import javax.swing.*;
import java.sql.*;
import java.io.*;
import javax.imageio.*;
import java.awt.image.*;
import java.awt.*;

final class EditProfileMember extends javax.swing.JInternalFrame {

    Object[][] tableObject=new Object[][]{};
    Systems systems=new Systems();
    Thread processThread=new Thread();
    Thread titleThread=new Thread();
    
    
    File pictureFile;
    String filePath="";
    Image pictureImage;
    ImageIcon pictureIcon;
    FileInputStream pictureInput;
    FileOutputStream pictureOutput;

    File signFile;
    String signFilePath="";
    Image signImage;
    ImageIcon signIcon;
    FileInputStream signInput;
    FileOutputStream signOutput;
    
    PreparedStatement statement;
    int numOfColumn=0;
    String[] tableColumn=new String[]{},defaultValue=new String[]{},typeValue=new String[]{},refTableValue=new String[]{},optionalValue=new String[]{};
    boolean editableCell[]=new boolean[]{};
    String[] tbAttribute=new String[]{};
    String[] tbType=new String[]{};
    String[] tbEditable=new String[]{};
    String multiRow="",profileTableCode="";
    /** Creates new form ViewCredit */
    public EditProfileMember() {
        initComponents();
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            tableObject,
            new String [] {
                "Attribute"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        reset();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jCancelButton = new javax.swing.JButton();
        jProfileNameComboBox = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jMemberCodeComboBox = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jUpdateButton = new javax.swing.JButton();
        jPictureButton = new javax.swing.JButton();
        jPictureCheckBox = new javax.swing.JCheckBox();
        jPanel3 = new javax.swing.JPanel();
        picLabel = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jCommentTextArea = new javax.swing.JTextArea();
        jLabel3 = new javax.swing.JLabel();
        jCommentCheckBox = new javax.swing.JCheckBox();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(openmoneyspinnersuite.OpenMS_Finance_App.class).getContext().getResourceMap(EditProfileMember.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setToolTipText(resourceMap.getString("Form.toolTipText")); // NOI18N
        setFrameIcon(resourceMap.getIcon("Form.frameIcon")); // NOI18N
        setName("Form"); // NOI18N
        setVisible(true);

        jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Attribute", "Value"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.setName("jTable1"); // NOI18N
        jTable1.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getColumn(0).setPreferredWidth(10);
        jTable1.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("jTable1.columnModel.title0")); // NOI18N
        jTable1.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("jTable1.columnModel.title4")); // NOI18N

        jCancelButton.setFont(resourceMap.getFont("jCancelButton.font")); // NOI18N
        jCancelButton.setIcon(resourceMap.getIcon("jCancelButton.icon")); // NOI18N
        jCancelButton.setText(resourceMap.getString("jCancelButton.text")); // NOI18N
        jCancelButton.setName("jCancelButton"); // NOI18N
        jCancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCancelButtonActionPerformed(evt);
            }
        });

        jProfileNameComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "-- Select --", "Item 2", "Item 3", "Item 4" }));
        jProfileNameComboBox.setName("jProfileNameComboBox"); // NOI18N
        jProfileNameComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jProfileNameComboBoxActionPerformed(evt);
            }
        });

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jMemberCodeComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "-- Select --", "Item 2", "Item 3", "Item 4" }));
        jMemberCodeComboBox.setName("jMemberCodeComboBox"); // NOI18N
        jMemberCodeComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMemberCodeComboBoxActionPerformed(evt);
            }
        });

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jUpdateButton.setIcon(resourceMap.getIcon("jUpdateButton.icon")); // NOI18N
        jUpdateButton.setText(resourceMap.getString("jUpdateButton.text")); // NOI18N
        jUpdateButton.setName("jUpdateButton"); // NOI18N
        jUpdateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jUpdateButtonActionPerformed(evt);
            }
        });

        jPictureButton.setText(resourceMap.getString("jPictureButton.text")); // NOI18N
        jPictureButton.setEnabled(false);
        jPictureButton.setName("jPictureButton"); // NOI18N
        jPictureButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jPictureButtonActionPerformed(evt);
            }
        });

        jPictureCheckBox.setName("jPictureCheckBox"); // NOI18N
        jPictureCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jPictureCheckBoxActionPerformed(evt);
            }
        });

        jPanel3.setBackground(resourceMap.getColor("jPanel3.background")); // NOI18N
        jPanel3.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        jPanel3.setName("jPanel3"); // NOI18N
        jPanel3.setPreferredSize(new java.awt.Dimension(140, 160));

        picLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        picLabel.setIcon(resourceMap.getIcon("picLabel.icon")); // NOI18N
        picLabel.setName("picLabel"); // NOI18N

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(picLabel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(picLabel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jCommentTextArea.setColumns(20);
        jCommentTextArea.setRows(5);
        jCommentTextArea.setEnabled(false);
        jCommentTextArea.setName("jCommentTextArea"); // NOI18N
        jScrollPane2.setViewportView(jCommentTextArea);

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jCommentCheckBox.setName("jCommentCheckBox"); // NOI18N
        jCommentCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCommentCheckBoxActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(583, Short.MAX_VALUE)
                .addComponent(jUpdateButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jCancelButton)
                .addContainerGap())
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 775, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jProfileNameComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 266, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel2))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 266, Short.MAX_VALUE)
                                    .addComponent(jMemberCodeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jCommentCheckBox)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 400, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jPictureButton, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPictureCheckBox))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jProfileNameComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jMemberCodeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(37, 37, 37)
                                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(21, 21, 21)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPictureCheckBox)
                            .addComponent(jPictureButton))
                        .addGap(10, 10, 10))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jCommentCheckBox)
                        .addGap(100, 100, 100)))
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 274, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jUpdateButton, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jCancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCancelButtonActionPerformed
        
        if(processThread.isAlive())
        {
            try{
                processThread.stop();
                titleThread.stop();
                System.out.println("Threads successfully stopped.");
            }catch(Exception e){System.out.println("Thread stopping error: "+e);}
            this.setTitle("::. Edit Profile Member");
            enableControls();
        }
        else
        dispose();
    }//GEN-LAST:event_jCancelButtonActionPerformed

private void jProfileNameComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jProfileNameComboBoxActionPerformed
        
        /* Execute thread */
        processThread=new Thread(new Runnable() {
            public void run() {
                disableControls();
                profileCodeSelected();
            }
        });
        titleThread=new Thread(new Runnable() {
            public void run() {
                while(processThread.isAlive())setProcessingTitle(titleThread);
                enableControls();
                System.out.println("viewItemThread stopped runing.");
            }
        });
        processThread.start();
        titleThread.start();
}//GEN-LAST:event_jProfileNameComboBoxActionPerformed
void profileCodeSelected()
{    
    String profileCode=jProfileNameComboBox.getSelectedItem().toString().trim();
    if(profileCode.equalsIgnoreCase("-- Select --"))
    {
        reset();
    }
    else{
        resetMemberCode();resetTable();jCommentTextArea.setText("");resetCheckBoxes();
    }
}
    void resetTable()
    {
         tableObject=new String[][]{};
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            tableObject,
            new String [] {
                "Attribute"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
         picLabel.setIcon(getImage(""));
    }                
    void resetTableCombo()
    {
        for(int x=0;x<typeValue.length;x++)
        {
            if(typeValue[x].equalsIgnoreCase("reference"))
            {
                String[] dbItem=systems.getTableDataArray(OpenMSApp.Database_A, " vwMerchant where vTable='"+refTableValue[x].trim().replace(' ', '_') +"'", 1);
                javax.swing.JComboBox combo=new javax.swing.JComboBox(dbItem);
                javax.swing.table.TableColumn comboColumn=jTable1.getColumnModel().getColumn(x+1);
                comboColumn.setCellEditor(new javax.swing.DefaultCellEditor(combo));            
            }
        }
    }
private void jMemberCodeComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMemberCodeComboBoxActionPerformed
    String memberCode=jMemberCodeComboBox.getSelectedItem().toString().trim();
    if(memberCode.equalsIgnoreCase("-- Select --"))reset();
    else{ refreshTable();picLabel.setIcon(getImage(memberCode));setMemberComment();}
}//GEN-LAST:event_jMemberCodeComboBoxActionPerformed
    void setMemberComment()
    {
        profileTableCode=systems.getValue(OpenMSApp.Database_A, "Select cReferenceTableCode from referenceTable where vName='"+jProfileNameComboBox.getSelectedItem().toString()+"'", 1);
        String comment=systems.getValue(OpenMSApp.Database_A, "Select tComment from ProfileMemberComment where cProfileMemberCode='"+jMemberCodeComboBox.getSelectedItem().toString()+"' and cTableCode='"+profileTableCode+"'", 1);
        if(comment==null)comment="";
        jCommentTextArea.setText(comment);
    }
    Icon getImage(String imagePath)
    {
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(openmoneyspinnersuite.OpenMSApp.class).getContext().getResourceMap(ViewProfileMembers.class);
        Icon displayedImage=resourceMap.getIcon("picLabel.icon");
        Icon stuImage=new ImageIcon(Configuration.profileImageDirPath+imagePath+".jpg");
        //getTreeLock().notify();
        if(stuImage.getIconHeight()!=-1)
        {
            displayedImage=stuImage;
        }
        return displayedImage;
    }
void resetMemberCode()
{
    String[] orderCode={};
    multiRow=systems.getValue(OpenMSApp.Database_A, "Select * from referenceTable where vName='"+jProfileNameComboBox.getSelectedItem().toString().trim()+"'", "cMultiRow");
    if(multiRow.equalsIgnoreCase("1"))
    {
        orderCode=systems.getColumn(OpenMSApp.Database_A,"Select distinct(c"+jProfileNameComboBox.getSelectedItem().toString().trim().replace(' ', '_')+"code) from "+jProfileNameComboBox.getSelectedItem().toString().trim().replace(' ', '_'),1);
        String[] orderCodeReset=new String[orderCode.length+1];
        for(int uu=1,k=0;uu<orderCode.length+1;uu++)
        orderCodeReset[uu]=orderCode[k++];
        orderCode=orderCodeReset;
    }
    else
        orderCode=systems.getTableDataArray(OpenMSApp.Database_A,jProfileNameComboBox.getSelectedItem().toString().trim().replace(' ', '_'),1);
    orderCode[0]="-- Select --";
    jMemberCodeComboBox.setModel(new DefaultComboBoxModel(orderCode));  
    jCommentTextArea.setText("");jCommentTextArea.setEnabled(false);jCommentTextArea.setEditable(false);jCommentCheckBox.setSelected(false);
}
private void jUpdateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jUpdateButtonActionPerformed
        // TODO add your handling code here:
        if(jProfileNameComboBox.getSelectedItem().equals("-- Select --"))
        {
            JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"Please Select Member ID ","ERROR !",JOptionPane.ERROR_MESSAGE);
            return;
        }
        if(jPictureCheckBox.isSelected()&&multiRow.equalsIgnoreCase("0")){
                if(filePath.isEmpty())
                {
                            JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"Please Browse Member's Picture ","ERROR !",JOptionPane.ERROR_MESSAGE);
                            return;
                }
                systems.deleteImage(Configuration.profileImageDirPath+"\\"+jProfileNameComboBox.getSelectedItem().toString().trim().replace(' ', '_') +"\\Image\\"+jMemberCodeComboBox.getSelectedItem().toString().trim()+".jpg");
                savePicture();
        }
        else{
                submitData();
                resetCheckBoxes();
                reset();
        }
}//GEN-LAST:event_jUpdateButtonActionPerformed

    
     void savePicture()
    {
        String employeeID=jMemberCodeComboBox.getSelectedItem().toString();
        File fileIn=new File(filePath);
        File fileOut=new File(Configuration.profileImageDirPath+employeeID.trim()+".jpg");
        if(fileOut.exists())
        {
            fileOut.delete();
        }    
        if(fileIn.renameTo(fileOut)==false)
        {
            String[] pictureFormat;
            try{
                pictureFormat=ImageIO.getReaderFormatNames();
                String formats="";
                int i=pictureFormat.length;
                for(int control=0;control<i;control++)
                {formats=formats+","+pictureFormat[control];}
                System.out.println(formats);
                System.out.println(fileOut.getAbsolutePath());
                System.out.println("FIN: "+fileIn.getAbsolutePath());
                //ImageReader im=ImageIO.getImageReader(null);
                RenderedImage imageBuffered=ImageIO.read(fileIn);
                ImageIO.write(imageBuffered,"JPG", fileOut);
                fileOut=null;
                fileIn=null;

                JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"Picture Successfully Saved !","Execution SUCCESSFUL !",JOptionPane.INFORMATION_MESSAGE);
            }
            catch(Exception e)
            {
                System.out.println("EditEmployee.savePicture() gave error! "+e);
            }
        }
        else
        {
                JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"Picture Successfully Saved !!","Execution SUCCESSFUL !",JOptionPane.INFORMATION_MESSAGE);

        }

    }

private void jPictureButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jPictureButtonActionPerformed
 
      try{
                JFileChooser chooseFile=new JFileChooser();
                chooseFile.showOpenDialog(this);
                pictureFile=chooseFile.getSelectedFile();
                filePath=pictureFile.getAbsolutePath();
                pictureIcon=new ImageIcon(filePath);
                pictureInput=new FileInputStream(filePath);
                System.out.println(filePath);                
                pictureImage=ImageIO.read(pictureInput);
                int iconHeight=pictureIcon.getIconHeight();
                int iconWidth=pictureIcon.getIconWidth();
                if(140>=iconHeight || 400>=iconWidth)
                {
                    picLabel.setIcon(pictureIcon);
                }
                else
                {
                    JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"Picture size is more than : \n        "+140+" * "+400,"Allocation ERROR !",JOptionPane.ERROR_MESSAGE);
                }
    }
   catch(Exception e){ System.out.println("EditProfileMember.browseButton gave error! ");}    
}//GEN-LAST:event_jPictureButtonActionPerformed

private void jPictureCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jPictureCheckBoxActionPerformed

    if(jPictureCheckBox.isSelected())
    {
        jPictureButton.setEnabled(true);
    }
    else
    {
        jPictureButton.setEnabled(false);
    }
}//GEN-LAST:event_jPictureCheckBoxActionPerformed

private void jCommentCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCommentCheckBoxActionPerformed

    if(jCommentCheckBox.isSelected())
    {
        jCommentTextArea.setEnabled(true);
        jCommentTextArea.setEditable(true);
    }
    else
    {
        jCommentTextArea.setEditable(false);
        jCommentTextArea.setEnabled(false);
    }
}//GEN-LAST:event_jCommentCheckBoxActionPerformed
    private void disableControls()
    {
        jUpdateButton.setEnabled(false);
        jProfileNameComboBox.setEnabled(false);
        jMemberCodeComboBox.setEnabled(false);
    }                                             
    private void enableControls()
    {
        jUpdateButton.setEnabled(true);
        jProfileNameComboBox.setEnabled(true);
        jMemberCodeComboBox.setEnabled(true);
        this.setTitle("::. Edit Profile Member");
    }           
    
     void resetCheckBoxes()
    {
        jPictureCheckBox.setSelected(false);
        jCommentCheckBox.setSelected(false);
        jCommentTextArea.setEnabled(false);
        jCommentTextArea.setEditable(false);
    }
    void resetProfile()
    {
        String[] orderCode=systems.getTableDataArray(OpenMSApp.Database_A,"vwProfile",2);
        orderCode[0]="-- Select --";
        jProfileNameComboBox.setModel(new DefaultComboBoxModel(orderCode));
    }
     void nillProfileMember()
     {
        String[] orderCode=new String[1];
        orderCode[0]="-- Select --";
        jMemberCodeComboBox.setModel(new DefaultComboBoxModel(orderCode)); 
     }
     void resetCombo()
     {
         picLabel.setIcon(getImage(""));
         resetProfile();
         nillProfileMember();     
     }
     void reset()
     {
         picLabel.setIcon(getImage(""));
         jCommentTextArea.setText("");
         jPictureButton.setEnabled(false);
         resetProfile();
         nillProfileMember();
         resetTable();
         resetCheckBoxes();
     }
     
     void refreshTable()
     {
        String memberCode=jMemberCodeComboBox.getSelectedItem().toString();
        if(memberCode.equalsIgnoreCase("-- Select --"))
        {
            JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"PLEASE SELECT A MEMBER CODE !","ERROR !",JOptionPane.ERROR_MESSAGE);
            return;
        }
        /*
        //retrieve table attribute (columns)
        String[] tbAttribute=systems.getColumn(OpenMSApp.Database_A, "Select * from profile where cProfileCode='"
                +jProfileNameComboBox.getSelectedItem().toString()+"'", 2);
        numOfColumn=tbAttribute.length;
        //use a to determine the number of rows in a table
        String[] a=systems.getTableDataArrayNum(OpenMSApp.Database_A,"Select * from "+jProfileNameComboBox.getSelectedItem().toString().trim()
                +" where c"+jProfileNameComboBox.getSelectedItem().toString().trim()+"Code ='"+jMemberCodeComboBox.getSelectedItem().toString()+"'",1);
        int numOfRow=a.length;        
        String dbMemberRecord[]=new String[]{};
        //using for loop to retrieve table records
        for(int x=0;x<a.length;x++)
        dbMemberRecord[x]=systems.getValue(OpenMSApp.Database_A,"Select * from "+jProfileNameComboBox.getSelectedItem().toString().trim()
                +" where c"+jProfileNameComboBox.getSelectedItem().toString().trim()+"Code ='"+jMemberCodeComboBox.getSelectedItem().toString()+"'",x+1);
        
        tableObject=new Object[2][numOfRow];
        tableObject[0]=tbAttribute;
        tableObject[1]=dbMemberRecord;
        */
        String profileCode=systems.getValue(OpenMSApp.Database_A, "Select * from referenceTable where vName='"+jProfileNameComboBox.getSelectedItem().toString() +"'", 1);
        //retrieve table attribute (columns)
        String pr[][]=systems.getColumn(OpenMSApp.Database_A, "Select * from profile where cProfileCode='"+profileCode+"' order by cProfileCode,iSerial", 2,3,8);
        tbAttribute=pr[0];
        tbType=pr[1];
        tbEditable=pr[2];
        //invert attribute and add one more column for profile member code
        
        /* DB recording now ok Invertion no more needed. Only used to increase attribute by one
        for(int uu=1,k=tbAttribute.length;uu<tbAttribute.length+1;uu++)
            tbAttributeInvert[uu]=tbAttribute[--k];*/
        String[] tbAttributeInvert=new String[tbAttribute.length+1];
        
        for(int uu=1,k=0;uu<tbAttribute.length+1;uu++)
        tbAttributeInvert[uu]=tbAttribute[k++];
        tbAttributeInvert[0]="S/N";
        tbAttribute=tbAttributeInvert;
        //number of columns available in table
        numOfColumn=tbAttribute.length;
        //use a to determine the number of rows in a table
        String[] a=systems.getTableDataArrayNum(OpenMSApp.Database_A,jProfileNameComboBox.getSelectedItem().toString().trim().replace(' ', '_')
                +" where c"+jProfileNameComboBox.getSelectedItem().toString().trim().replace(' ','_') +"Code ='"+jMemberCodeComboBox.getSelectedItem().toString()+"'",1);
        int numOfRow=a.length;
        //initialize a to hold table records
        String[][] dbMemberRecord=new String[tbAttribute.length][numOfRow];
        //using for loop to retrieve table records
        for(int x=0;x<tbAttribute.length;x++)
        dbMemberRecord[x]=systems.getTableDataArrayNum(OpenMSApp.Database_A,jProfileNameComboBox.getSelectedItem().toString().trim().replace(' ', '_') +" where c"+jProfileNameComboBox.getSelectedItem().toString().trim().replace(' ', '_') +"Code ='"+jMemberCodeComboBox.getSelectedItem().toString()+"'",x+3);
        int x=0;
        tableObject=new Object[numOfRow][numOfColumn];    
        while(x<numOfRow)
        {
            for(int xu=0;xu<numOfColumn;xu++){
                if(xu==0)tableObject[x][0]=(x+1);
                else
                tableObject[x][xu]=dbMemberRecord[xu][x];
            }
            x+=1;
            if(x==numOfRow){break;}
       }

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            tableObject,tbAttribute
        ) {
            boolean[] canEdit = systems.convertBinaryStringArrayToBooleanArrayFLead(tbEditable);

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.getColumnModel().getColumn(0).setMaxWidth(40);
     }
    void print()
    {
        try
        {
            jTable1.print();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
                       
    private void setProcessingTitle(Thread thread)
    {
        try{
            this.setTitle("::. Edit Profile Member - Processing Request");
            thread.sleep(500);
            this.setTitle("::. Edit Profile Member - Processing Request.");
            thread.sleep(500);
            this.setTitle("::. Edit Profile Member - Processing Request..");
            thread.sleep(500);
            this.setTitle("::. Edit Profile Member - Processing Request...");
            thread.sleep(500);
        }catch(Exception e){System.out.println("Title thread"+e);}
    }
    void submitData()
    {
        String comment=systems.getValue(OpenMSApp.Database_A, "Select tComment from vwProfile where vName='"+jProfileNameComboBox.getSelectedItem().toString()+"'","tComment");
            
        //Connect.createMSSQLConnection();
            Connect.changeDB(OpenMSApp.Database_A);
        for(int xx=0;xx<tableObject.length;xx++)
        {
            String setQuery="";
            for(int x=1;x<numOfColumn;x++)
            {
                if(tbType[x-1].equalsIgnoreCase("Integer")||tbType[x-1].equalsIgnoreCase("Decimal"))
                setQuery=setQuery+tbAttribute[x].replace(' ', '_') +"="+jTable1.getValueAt(xx,x);
                else
                setQuery=setQuery+tbAttribute[x].replace(' ', '_') +"='"+jTable1.getValueAt(xx,x)+"'";
                if(x<numOfColumn-1)setQuery=setQuery+",";
            }
            statement=Connect.createStatement("UPDATE "+jProfileNameComboBox.getSelectedItem().toString().trim().replace(' ', '_') +" SET "+setQuery
                    +" WHERE c"+jProfileNameComboBox.getSelectedItem().toString().replace(' ', '_') +"code='"+jMemberCodeComboBox.getSelectedItem().toString()
                    +"' and iSerial="+jTable1.getValueAt(1,0));

            Connect.executeUpdateStatement(statement);
        }
        if(comment.equalsIgnoreCase("1"))
        {
            System.out.println("right here!");
            statement=Connect.createStatement(Connect.procInit+" prnUpdProfileMemberComment"+Connect.procInitStart+"?,?,?"+Connect.procInitEnd);
            try{
            statement.setString(1,jMemberCodeComboBox.getSelectedItem().toString());
            statement.setString(2,profileTableCode.trim());
            statement.setString(3,jCommentTextArea.getText().trim());
            }
            catch(Exception e)
            {
                System.out.println("Error updating profileMemberComment:"+e);
            }

            Connect.executeUpdateStatement(statement);
        }
        Connect.alert="Y";
        Connect.closeConnection();    
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jCancelButton;
    private javax.swing.JCheckBox jCommentCheckBox;
    private javax.swing.JTextArea jCommentTextArea;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JComboBox jMemberCodeComboBox;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JButton jPictureButton;
    private javax.swing.JCheckBox jPictureCheckBox;
    private javax.swing.JComboBox jProfileNameComboBox;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JButton jUpdateButton;
    private javax.swing.JLabel picLabel;
    // End of variables declaration//GEN-END:variables

}
